package cn.stephen.study.demoproject.service.impl;

import cn.stephen.study.demoproject.dao.dbtwo.StationEquipmentMapper;
import cn.stephen.study.demoproject.dao.dbtwo.SBJCYJMapper;
import cn.stephen.study.demoproject.entity.SBJCYJ;
import cn.stephen.study.demoproject.service.SBJCYJService;
import cn.stephen.study.demoproject.util.HttpClient4;
import cn.stephen.study.demoproject.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SBJCYJServiceImpl implements SBJCYJService {

    @Autowired
    private SBJCYJMapper sbjcyjMapper;
    @Autowired
    private StationEquipmentMapper stationEquipmentMapper;

    @Override
    public List<SBJCYJ> findAll() {
        return sbjcyjMapper.getAll();
    }

    @Override
    public List<SBJCYJ> findAlls(String id, Integer type, String startTime, String endTime, Integer warningType) {
        List<SBJCYJ> sbjcyj = new ArrayList<>();
        //获取已有站点得数据
//        List<String> stationCodes = stationEquipmentMapper.findAllJSCJ204A050();
        List<String> stationCodes=new ArrayList<>();
        stationCodes.add("42098110800204000000");
        stationCodes.add("42092310700904000000");
        stationCodes.add("42098100200104000001");
        stationCodes.add("42098100200604000000");
        stationCodes.add("42098100500104000000");
        if (id.equals("")) {
            id = null;
        }
        if(type!=5){
            //type为5 时，是导出报表
            if (endTime==null||startTime.equals("")) {
                startTime = null;
            }else{
                startTime += " 00:00:00";
            }
            if (endTime==null||endTime.equals("")) {
                endTime = null;
            }else{
                endTime += " 23:59:59";
            }
        }
        stationCodes.removeAll(Collections.singleton(null));
        for(int i=0;i< stationCodes.size()/1000+1;i++){
            List<String> codes=new ArrayList<>();
            if(i==stationCodes.size()/1000){
                codes=stationCodes.subList(i*1000,stationCodes.size()-1);
            }else{
                codes=stationCodes.subList(i*1000,(i+1)*1000-1);
            }
            List<SBJCYJ> sbjcyj1 = new ArrayList<>();
            sbjcyj1 = sbjcyjMapper.findByTimesAndType(id, startTime, endTime, warningType,codes);
            sbjcyj.addAll(sbjcyj1);
        }
        return sbjcyj;
    }

    /**
     * 获取站点下的数据
     * type：1周报表  2 月报表
     * uniqure_code ： 设别唯一编码
     *
     * @return
     */
    @Override
    public String getValue(Integer type, String uniqure_code, Integer equipmentType) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = "";
        String endTime = "";
        if (type == 1) {
            //周报表，获取上周数据
            Date beginDayOfWeek = TimeUtil.getBeginDayOfLastWeek();
            Date endDayOfWeek = TimeUtil.getEndDayOfLastWeek();
            startTime = sdf.format(beginDayOfWeek);
            endTime = sdf.format(endDayOfWeek);
        } else {
            //月报表 ，获取月数据
            Date beginDayOfMonth = TimeUtil.getBeginDayOfLastMonth();
            Date endDayOfMonth = TimeUtil.getEndDayOfLastMonth();
            startTime = sdf.format(beginDayOfMonth);
            endTime = sdf.format(endDayOfMonth);
        }
//        startTime="2020-06-01 00:00:00";
//        endTime="2020-08-01 00:00:00";
        String name = "";
        if (equipmentType == 204) {
            //GNSS
            name = "query204a";
        } else if (equipmentType == 212) {
            //深部位移
            name = "query212a";
        } else if (equipmentType == 214) {
            //地下水位
            name = "query214a";
        }
        String url = "http://192.168.9.59:801/apis/result/" + name + "/?startTime=" +
                startTime + "&endTime=" + endTime + "&uniqure_code=" + uniqure_code + "&adapt=false";
        url = url.replaceAll(" ", "%20");
        return HttpClient4.doGet(url);
    }

    //获取某个日期的开始时间
    public static Date getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Date(calendar.getTimeInMillis());
    }

    // 获取某个日期的结束时间
    public static Date getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Date(calendar.getTimeInMillis());
    }

}
